<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
<style>
    body{
        margin: 0;
    }
    div {
        height: 200px;
    }
    .a {
        background-color: #00a1cc;
    }
    .b {
        background-color: #cc7d00;
        position: relative;
    }
    .c {
        background-color: #0d000a;
        color: white;
        position: absolute;
        width:100%;
        height:30px;
    }
    .d {
        background-color: #2300cc;
    }
    .fudong {
        position: fixed;
        top:10px;
        background-color: #0d000a;
        color: white;
        width:100%;
    }
</style>
</head>
<body>
<div class="a">aa</div>
<div class="a">aa</div>
<div class="b" id="a2">如何计算b离窗口的距离
    <div class="c" id="a3">cc</div>
</div>

<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<div class="d">dd</div>
<script>
//    var a2 = document.getElementById('a2');
//    var a3 = document.getElementById('a3');
////    alert(a3.offsetLeft);
////    alert(a3.offsetTop);
//    window.onscroll = function(){
//        var bodyScrollTop = document.body.scrollTop;
//        if (bodyScrollTop>=a2.offsetTop){
//            a3.className = 'fudong';
//        } else {
//            a3.className = 'c'
//        }
//        console.log(a2.offsetTop);
//        console.log(a3.offsetTop);
//        console.log(document.body.scrollTop);
//    }
document.body.onscroll = function(){
       console.log( document.body.scrollTop)
        var st = document.body.scrollTop-a2.offsetTop
        if (st>0){
               a3.className = 'fudong'
        } else {
            a3.className = 'c'
        }
    }
</script>
</body>
</html>